package com.ebpm.designer.line
{
	import com.ebpm.designer.WF;
	import com.ebpm.designer.WP;
	import com.ebpm.designer.WorkPanel;
	import com.ebpm.designer.dir.LineEndDir;
	import com.ebpm.designer.dir.LineFromDir;
	import com.ebpm.designer.dir.LineMiddleDir;
	
	/**
	 * 
	 * 底部节点 往下的线
	 * 
	 **/
	public class RRLine extends Line
	{
		public var p1:Point;
		public var p2:Point;
		public var startP1:Point;
		public var startP2:Point;
		public function RRLine()
		{
			this.resizeFromDir = new LineFromDir(this, "upDownIcon");
			this.resizeToDir = new LineEndDir(this, "upDownIcon");
			this.middleDir = new LineMiddleDir(this, "leftRightIcon");
			this.lineHeadType = "left";
			super();
		}
		
		override protected function initPoints(fromX:Number, fromY:Number, x:Number, y:Number):void {
			var itemSize:Number = WF.ITEM_SIZE;
			var showX:Number = x;
			if(x < fromX) showX = fromX;
			p1 = new Point(showX + WF.LINE_EXT_SIZE + (itemSize / 2) + this.resizeMiddleExtra, fromY + (itemSize / 2) + this.resizeFromExtraY);
			p2 = new Point(showX + WF.LINE_EXT_SIZE + (itemSize / 2) + this.resizeMiddleExtra, y + (itemSize / 2) + this.resizeToExtraY);
			this.points.addItem(p1);
			this.points.addItem(p2);
		}
		
		override public function repaintLine(type:String = "to"):void {
			var fromX:Number = this.sourceItem.nodeContainer.getRightLineX();
			var fromY:Number = this.sourceItem.nodeContainer.getRightLineY();
			var toX:Number = this.targetItem.nodeContainer.getRightLineX();
			var toY:Number = this.targetItem.nodeContainer.getRightLineY();
			//先将list中所有的点remove掉
			this.points.removeAll();
			
			//判断是否越界
			if(
				(this.sourceItem.nodeContainer.y < this.targetItem.nodeContainer.y && this.sourceItem.nodeContainer.x < this.targetItem.nodeContainer.x && (this.targetItem.nodeContainer.y <= this.startPoint.y)) ||                                    
				(this.sourceItem.nodeContainer.y < this.targetItem.nodeContainer.y && this.sourceItem.nodeContainer.x > this.targetItem.nodeContainer.x && (this.sourceItem.nodeContainer.y + this.sourceItem.nodeContainer.height >= this.endPoint.y)) ||
				(this.sourceItem.nodeContainer.y > this.targetItem.nodeContainer.y && this.sourceItem.nodeContainer.x < this.targetItem.nodeContainer.x && (this.targetItem.nodeContainer.y + this.targetItem.nodeContainer.height >= this.startPoint.y)) ||
				(this.sourceItem.nodeContainer.y > this.targetItem.nodeContainer.y && this.sourceItem.nodeContainer.x > this.targetItem.nodeContainer.x && (this.sourceItem.nodeContainer.y <= this.endPoint.y)) 
			) {
				WF.wp.removeElement(this);
				this.sourceItem.sourceLines.removeItem(this);
				this.targetItem.targetLines.removeItem(this);
				//根据UserTaskContaier的坐标x 转换相应的线
				if(this.sourceItem.nodeContainer.getDownLineX() < this.targetItem.nodeContainer.getDownLineX()) {
					var line:Line = new RLLine().paint(fromX, fromY, this.targetItem.nodeContainer.getLeftLineX(), this.targetItem.nodeContainer.getLeftLineY());
					//fromItem 点不变
					//toItem 的点变了，的是UserTaskContaier的topItem的toLines添加
					this.targetItem.nodeContainer.leftItem.targetLines.addItem(line);
					line.targetItem = this.targetItem.nodeContainer.leftItem;
				} else {
					var line:Line = new LRLine().paint(this.sourceItem.nodeContainer.getLeftLineX(), this.sourceItem.nodeContainer.getLeftLineY(), this.targetItem.nodeContainer.getRightLineX(), this.targetItem.nodeContainer.getRightLineY());
					this.targetItem.nodeContainer.rightItem.targetLines.addItem(line);
					line.targetItem = this.targetItem.nodeContainer.rightItem;
				}
				line.sourceItem = this.sourceItem;
				this.sourceItem.sourceLines.addItem(line);
			}
			
			//没有resize 线之前，越界了，p1、p2的Y不动
			else if(!isAlreadyResize && fromX >= toX) {
				if(type == "to") {
					toOverPaint(fromX, fromY, toX, toY);
				} else {
					fromOverPaint(fromX, fromY, toX, toY);
				}
			}
			else {
				//如果没有resize 线，重画线
				if(!isAlreadyResize) {
					paint(fromX, fromY, toX, toY);
				} else { //如果resize过了线
					if(this.endPoint.x + WF.LINE_HEAD_W >= this.p2.x) {
						paint(fromX, fromY, toX, toY);
						isAlreadyResize = false;
					}
					else if(this.startPoint.x >= this.p1.x) {  //判断线是否向下越界了
						toOverPaint(fromX, fromY, toX, toY);
						isAlreadyResize = false;
					} 
					else {
						toOverPaint(fromX, fromY, toX, toY);
					}
				}
			}
			
		}
		
		public function fromOverPaint(fromX:Number, fromY:Number, x:Number, y:Number):void {
			var itemSize:Number = WF.ITEM_SIZE;
			startPoint = new Point(fromX + (itemSize / 2), fromY + (itemSize / 2) + this.resizeFromExtraY);
			this.points.addItem(startPoint);
			p1 = new Point(fromX + WF.LINE_EXT_SIZE + (itemSize / 2), fromY + (itemSize / 2) + this.resizeFromExtraY);
			p2 = new Point(fromX + WF.LINE_EXT_SIZE + (itemSize / 2), y + (itemSize / 2) + this.resizeToExtraY);
			this.points.addItem(p1);
			this.points.addItem(p2);
			endPoint = new Point(x + (itemSize / 2), y + (itemSize / 2) + this.resizeToExtraY);
			this.points.addItem(endPoint);
			this.drawLine();
			//如果重新画整条线的话,必须把resizeMiddleExtra设置为0
			this.resizeMiddleExtra = 0;
		}
		
		//移动to节点时 重画方法
		public function toOverPaint(fromX:Number, fromY:Number, x:Number, y:Number):void {
			var itemSize:Number = WF.ITEM_SIZE;
			startPoint = new Point(fromX + (itemSize / 2), fromY + (itemSize / 2) + this.resizeFromExtraY);
			this.points.addItem(startPoint);
			//p1、p2的 坐标Y保持不变
			p1 = new Point(this.p1.x, fromY + (itemSize / 2) + this.resizeFromExtraY);
			p2 = new Point(this.p2.x, y + (itemSize / 2) + this.resizeToExtraY);
			this.points.addItem(p1);
			this.points.addItem(p2);
			endPoint = new Point(x + (itemSize / 2), y + (itemSize / 2) + this.resizeToExtraY);
			this.points.addItem(endPoint);
			this.drawLine();
			//如果重新画整条线的话,必须把resizeMiddleExtra设置为0
			this.resizeMiddleExtra = 0;
		}
		
		override public function resizeMiddleItem():void {
			if(this.isFirstMove) {
				this.startP1 = this.p1;
				this.startP2 = this.p2;
				this.isFirstMove = false;
				this.resizeStartExtra = this.resizeMiddleExtra;
			}
			//			if(p2.y - this.toItem.userTaskContainer.getDownLineY() <= WF.LINE_EXT_SIZE) {
			//				return ;
			//			}
			this.resizeMiddleExtra = this.resizeStartExtra + WP.mouseChangeX;
			this.points.removeAll();
			this.points.addItem(this.startPoint);
			this.p1 = new Point(this.startP1.x + WP.mouseChangeX, this.startP1.y);
			this.p2 = new Point(this.p1.x, this.startP2.y);
			this.points.addItem(this.p1);
			this.points.addItem(this.p2);
			this.points.addItem(this.endPoint);
			this.drawLine();
			this.middleDir.x = this.p1.x - (WF.ITEM_SIZE / 2);
		}
		
		//拖拽框移动
		override public function resizeFromItem():void {
			if(this.isFirstMove) {
				this.resizeStartExtra = this.resizeFromExtraY;
				this.isFirstMove = false;
			}
			this.resizeFromExtraY = this.resizeStartExtra + WP.mouseChangeY;
			this.points.removeAll();
			var fromX = this.sourceItem.nodeContainer.getRightLineX();
			var fromY = this.sourceItem.nodeContainer.getRightLineY();
			var toX = this.targetItem.nodeContainer.getRightLineX();
			var toY = this.targetItem.nodeContainer.getRightLineY();
			this.toOverPaint(fromX, fromY, toX, toY);
		}
		
		
		override public function resizeEndItem():void {
			if(this.isFirstMove) {
				this.resizeStartExtra = this.resizeToExtraY;
				this.isFirstMove = false;
			}
			this.resizeToExtraY = this.resizeStartExtra + WP.mouseChangeY;
			//			Alert.show("aaaaaaaa");
			this.points.removeAll();
			var fromX = this.sourceItem.nodeContainer.getRightLineX();
			var fromY = this.sourceItem.nodeContainer.getRightLineY();
			var toX = this.targetItem.nodeContainer.getRightLineX();
			var toY = this.targetItem.nodeContainer.getRightLineY();
			this.toOverPaint(fromX, fromY, toX, toY);
			this.movingTxtByLine();
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}